import { UserDesc } from '@/store/user'

export namespace userAlipay {


    export const login = async (): Promise<string> => {
    	return new Promise((resolve,reject)=>{
    		uni.login({
    		  provider: 'weixin', 
    		  success:  (loginRes)=> {
    		    console.log('weapp login',loginRes);
    			if (!loginRes.code) {
    			    console.error('登录失败！' + loginRes.errMsg)
    			    reject('登录失败,请重试!');
    			}else{
    				const token = loginRes.code ?? '';
    				resolve(token)
    			}
    		  },
    		  fail:(error)=>{
    			  reject(error);
    		  }
    		});
    	})
    }

    export const bindMobile = async (): Promise<string> => {
        return new Promise((resolve, reject) => {
            my.getPhoneNumber({
                success: async (resAuth:any) => {
                    try {
                        console.log('my.getPhoneNumber', resAuth);
                        let encryptedData = resAuth.response;
                        // const res = await webAPIs.v1.alipay.decryptPhoneNumber.call({ encryptedData: encryptedData });
						const res :any={};
                        const mobile = res.data.data.phoneNumber;
                        if (!mobile) {
                            return reject('获取手机号失败');
                        }
                        return resolve(mobile);
                    } catch (error) {
                        return reject(error);
                    }
                },
                fail: (error:any) => {
                    reject(error);
                }
            })
        })
    }
    // export const updateUser = async (updateUser: webAPIsV1models.AuthRegisterInput) => {
    //     try {
    //         const res = await webAPIs.v1.alipay.updateUser.call(updateUser);
    //         userStore.setUserInfo(res.data);
    //         return Promise.resolve(res);
    //     } catch (error) {
    //         return Promise.reject(error);
    //     }
    // }
    export const getUserProfile = (): Promise<UserDesc> => {
        return new Promise((resolve, reject) => {
            my.getAuthCode({
                scopes: 'auth_user',
                success: (resAuth:any) => {
                    console.log('getAuthCode 授权码', resAuth.authCode);
                    my.getOpenUserInfo({
                        success: (res:any) => {
                            let userInfo = JSON.parse(res.response).response
                            if (userInfo.nickName && userInfo.avatar) {
                                resolve({
                                    nickName: userInfo.nickName,
                                    avatar: userInfo.avatar,
                                })
                            } else {
                                reject(userInfo.msg);
                            }
                            console.log('用户', userInfo);
                        },
                        fail: (error:any) => {
                            reject(error)
                        }
                    })
                },
                fail: (error:any) => {
                    reject('授权失败,' + error);
                }
            })

        })
    }

}